package classroom;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

import com.project.institute.DAO.Institute;
import com.project.trac.R;

import core.modules.DatabaseConnector;

public class LauncherStudent extends Institute {

	private List<String> list_subjectName = new ArrayList<String>();
	private List<String> list_scheduleID = new ArrayList<String>();
	private List<String> list_subjectID = new ArrayList<String>();
	private List<String> list_schUID = new ArrayList<String>();
	private int scheduleUID;
	
	private Spinner spinner;
	private Button btn_go;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.student_selector);
		setTitle("Subject selection");

		spinner = (Spinner) findViewById(R.id.catalog_spinner);
		
		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				
				String subjectName = parent.getItemAtPosition(position).toString();
				scheduleUID = Integer.valueOf(list_schUID.get(list_subjectName.indexOf(subjectName)));
				
				setSubjectID(Integer.valueOf(list_subjectID.get(list_subjectName.indexOf(subjectName))));
				setScheduleID(Integer.valueOf(list_scheduleID.get(list_subjectName.indexOf(subjectName))));
				setSubjectName(subjectName);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {}
		}); 

		btn_go = (Button) findViewById(R.id.go);
		
		btn_go.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivity(new Intent(LauncherStudent.this, ActivityManager.class).putExtra("SCHEDULE_UID", scheduleUID));
			}
		});
		
		new AsynchronousTask().execute();

	}
	
	@Override
	public void onAttachedToWindow() {
		super.onAttachedToWindow();
		Window window = getWindow();
		window.setFormat(PixelFormat.RGBA_8888);
	}
	
	@Override
	public void onBackPressed() {
		super.onBackPressed();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	        case R.id.menu_logout:
	            Logout(this);
	            return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
	
	private class AsynchronousTask extends AsyncTask<String, String, String> {

		HashMap<String, String> m;
		ArrayAdapter<String> arrayAdapter;

		ProgressDialog progressDialog;

		protected void onPreExecute() {
			super.onPreExecute();

			progressDialog = new ProgressDialog(LauncherStudent.this);
			progressDialog.setMessage("please wait...");
			progressDialog.setIndeterminate(true);
			progressDialog.setCancelable(true);
			progressDialog.show();

		}

		protected String doInBackground(String... args) {
		
			try {
			 ResultSet rs = DatabaseConnector.getStatement().executeQuery(
						"SELECT gradesheet.sid, curriculum.id  AS 'cid', curriculum.catalognumber,  schedule.uid"
						+ " FROM gradesheet"
						+ " INNER JOIN schedule ON schedule.id = gradesheet.sid"
						+ " INNER JOIN curriculum ON gradesheet.cid = curriculum.id"
						+ " WHERE isApproved = 1 AND gradesheet.uid = " + getUserID());

				while (rs.next()) {

					m = new HashMap<String, String>();
					m.put("k1", rs.getString("catalognumber"));

					list_subjectName.add(rs.getString("catalognumber"));
					list_subjectID.add(rs.getString("cid"));
					list_scheduleID.add(rs.getString("sid"));
					list_schUID.add(rs.getString("schedule.uid"));
					
				}

		} catch (SQLException e) {
			DatabaseConnector.Logger(getClass(), "doInBackground", e);
		} 
			return null;
		}

		protected void onPostExecute(String postExecute) {

			progressDialog.dismiss();

			runOnUiThread(new Runnable() {

				public void run() {

					arrayAdapter = new ArrayAdapter<String>(
							LauncherStudent.this,
							android.R.layout.simple_spinner_item, list_subjectName);
					arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
					spinner.setAdapter(arrayAdapter);
				}
			});
		}

	}
	
}